package com.intelligentmeterreadingsys.waterlist.copywater.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.WaterlistPoVo;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.waterlist.copywater.db.CopyWaterDao;

import dbhelper.XmlNameHelper;

/**
 * 抄表时选择表册例表
 * 
 * @author king
 * 
 */
public class CopyWaterListActivity extends Activity {

    public List<WaterlistPoVo> list;

    public List<String> date;

    public SharedPreferenceUtil sp;

    private CopyWaterlistAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	setContentView(R.layout.listview);
	sp = new SharedPreferenceUtil(this);
	ListView listview = (ListView) this.findViewById(R.id.listView1);
	CopyWaterDao dao = new CopyWaterDao(this);
	list = dao.queryAllWaterList();
	dao.close();

	date = new ArrayList<String>();
	for (int i = 0; i < list.size(); i++) {

	    date.add((i + 1) + "  表册名:" + list.get(i).getName());

	}
	adapter = new CopyWaterlistAdapter(date, this);
	listview.setAdapter(adapter); // 自动为id是list的ListView设置适配器
	listview.setFooterDividersEnabled(true);
	listview.setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		String waterlistid = getWaterlistID(list.get(arg2).getNumber());
		sp.insertValue(XmlNameHelper.QUERYWATERLIST, waterlistid);
		Intent intent = new Intent(CopyWaterListActivity.this, CopyWaterRFIDActivity.class);
		startActivity(intent);
	    }
	});

    }

    public String getWaterlistID(String Name) {
	for (int i = 0; i < list.size(); i++) {
	    if (Name.equals(list.get(i).getName())) {
		return list.get(i).getNumber();
	    }
	}
	return null;
    }
}
